﻿using CinemaManagement.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CinemaManagement.Utility;

namespace CinemaManagement.Controllers
{
    public class AdminController : Controller
    {
        // GET: Admin
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.movies = GetMovies();
            ViewBag.MovieNoFind = "没找到";
            ViewBag.TableFooter = "到底了";
            return View();
        }

        [HttpPost]
        public ActionResult Index(string name, string type)
        {
            ViewBag.movies = GetMovies(name, type);
            ViewBag.MovieNoFind = "没找到";
            ViewBag.TableFooter = "到底了";
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(Movie model)
        {

            if (ModelState.IsValid)
            {
                DbHelper.Instance.Db.Movie.Add(model);
                DbHelper.Instance.Db.SaveChanges();
            }
            return View();
        }


        public ActionResult Modify()
        {
            return View();
        }
        public ActionResult Remove()
        {
            return View();
        }
        public ActionResult Delete(int moiveId)
        {

            DeleteMovie (moiveId);
            //TODO:退订
            return RedirectToAction("Index");
        }

        private List<Movie> GetMovies()
        {
            return DbHelper.Instance.Db.Movie.ToList();
        }
        private List<Movie> GetMovies(string name, string type)
        {
            List<Movie> movies = DbHelper.Instance.Db.Movie.ToList();
            if (!string.IsNullOrEmpty(name))
            {
                movies = movies.Where(m => m.movieName.IndexOf(name) > -1).ToList();
            }
            if (!string.IsNullOrEmpty(type))
            {
                movies = movies.Where(m => m.movieType.Contains(type)).ToList();
            }
            return movies;
        }
        private void DeleteMovie(int moiveId)
        {
            DbHelper.Instance.Db.Movie.Remove(DbHelper.Instance.Db.Movie.FirstOrDefault(o => o.movieId == moiveId));
            DbHelper.Instance.Db.SaveChanges();

        }
    }
}